package com.module.guide.ui.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.module.guide.R;
import com.module.guide.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private ViewPager mGuideContainer;
    private LinearLayout mGuideViewContender;
    private Button mGuideEnterBtn;
    private List<ImageView> imageViews;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initListener() {
        mGuideEnterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getBaseContext(),MainActivity.class));
                finish();
            }
        });
    }

    @Override
    protected void initView() {
        mGuideContainer =findViewById(R.id.guide_list);
        mGuideViewContender=findViewById(R.id.guide_list_views);
        mGuideEnterBtn=findViewById(R.id.guide_enter_btn);
        int ids[]=new int[]
                {
                        R.mipmap.guide1,
                        R.mipmap.guide2,
                        R.mipmap.guide3,
                        R.mipmap.guide4,
                        R.mipmap.guide5

                };
        imageViews=new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            ImageView imageView =new ImageView(this);
            imageView.setBackgroundResource(ids[i]);
            imageViews.add(imageView);

        }
        mGuideContainer.setAdapter(new MyGuideAdapter());
        mGuideContainer.addOnPageChangeListener(this);
        ((View)mGuideViewContender.getChildAt(0)).setBackgroundResource(R.drawable.shape_guide_viewsh);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position==imageViews.size()-1)
        {
            mGuideEnterBtn.setVisibility(View.VISIBLE);
        }else {

            mGuideEnterBtn.setVisibility(View.GONE);
        }
        for (int i = 0; i < mGuideViewContender.getChildCount(); i++) {
            if(position==i)
            {

                ((View)mGuideViewContender.getChildAt(i)).setBackgroundResource(R.drawable.shape_guide_viewsh);
            }else {
                ((View)mGuideViewContender.getChildAt(i)).setBackgroundResource(R.drawable.shape_guide_views);

            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class  MyGuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
           return imageViews.size();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(imageViews.get(position));
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView imageView=imageViews.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }
    }
}
